package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.IgnoreClientToken;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.datatypewordrel.DataTypeWordRelCondition;
import com.bwda.dsrs.filecenter.domain.po.datasensitivewords.DataSensitiveWordsPo;
import com.bwda.dsrs.filecenter.domain.condition.datasensitivewords.DataSensitiveWordsCondition;
import com.bwda.dsrs.filecenter.domain.form.datasensitivewords.DataSensitiveWordsQueryForm;
import com.bwda.dsrs.filecenter.domain.form.datasensitivewords.DataSensitiveWordsUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.datasensitivewords.DataSensitiveWordsCreateForm;
import com.bwda.dsrs.filecenter.domain.po.datatype.DataTypePo;
import com.bwda.dsrs.filecenter.domain.po.datatypewordrel.DataTypeWordRelPo;
import com.bwda.dsrs.filecenter.domain.vo.datasensitivewords.DataSensitiveWordsVo;
import com.bwda.dsrs.filecenter.service.DataSensitiveWordsService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.service.DataTypeService;
import com.bwda.dsrs.filecenter.service.DataTypeWordRelService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/datasensitivewords")
@Api(value = "/b/datasensitivewords", tags = "敏感字段表")
public class DataSensitiveWordsController extends BaseController {

	@Autowired
	private DataSensitiveWordsService dspDataSensitiveWordsService;

	@Autowired
	private DataTypeWordRelService dataTypeWordRelService;
	@Autowired
	private DataTypeService dataTypeService;

	@ApiOperation(value = "查询敏感字段表",notes = "查询敏感字段表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataSensitiveWordsVo> query(@ApiParam(value = "word_id", required = true)@RequestParam Integer wordId) throws BusinessServiceException {
		DataSensitiveWordsPo po = dspDataSensitiveWordsService.queryWithValid(wordId);
		DataSensitiveWordsVo vo = CopyUtil.transfer(po, DataSensitiveWordsVo.class);
		return getSuccessResult(vo);
	}

//	@IgnoreClientToken
//	@ApiOperation(value = "查询敏感字段表",notes = "查询敏感字段表",httpMethod = "GET")
//	@RequestMapping(value = "/match", method = {RequestMethod.GET, RequestMethod.POST})
//	public ResponseEntity<String> match(String text) throws BusinessServiceException {
//		String ids = dspDataSensitiveWordsService.match(text);
//		return getSuccessResult(ids);
//	}

	@ApiOperation(value = "查询敏感字段表数量",notes = "查询敏感字段表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DataSensitiveWordsQueryForm form) throws BusinessServiceException {
		DataSensitiveWordsCondition condition = this.getConditionByQueryForm(form);
		int count = dspDataSensitiveWordsService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询敏感字段表列表",notes = "查询敏感字段表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DataSensitiveWordsVo>> queryList(@ModelAttribute@Valid DataSensitiveWordsQueryForm form) throws BusinessServiceException {
		DataSensitiveWordsCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<DataSensitiveWordsPo> poList = dspDataSensitiveWordsService.queryList(condition);
		List<DataSensitiveWordsVo> voList = CopyUtil.transfer(poList, DataSensitiveWordsVo.class);
		return getSuccessResult(getListResponse(voList));
	}


	@IgnoreClientToken
	@ApiOperation(value = "查询敏感字段表列表",notes = "查询敏感字段表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryListForType", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DataSensitiveWordsVo>> queryListForType(@ModelAttribute@Valid DataSensitiveWordsQueryForm form) throws BusinessServiceException {
		DataSensitiveWordsCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<DataSensitiveWordsPo> poList = dspDataSensitiveWordsService.queryList(condition);
		List<DataSensitiveWordsVo> voList = null;
		if (!CollectionUtils.isEmpty(poList)){
			voList = CopyUtil.transfer(poList, DataSensitiveWordsVo.class);
			DataTypeWordRelCondition condition1 = new DataTypeWordRelCondition();
			condition1.setTypeId(form.getTypeId());
			List<DataTypeWordRelPo> list =dataTypeWordRelService.queryList(condition1);
			if (!CollectionUtils.isEmpty(list)){
				for (DataSensitiveWordsVo dataSensitiveWordsVo:voList){
					for (DataTypeWordRelPo dataTypeWordRelPo:list){
						if (dataTypeWordRelPo.getWordId().equals(dataSensitiveWordsVo.getWordId())){
							dataSensitiveWordsVo.setCheckFlag(true);
							break;
						}else {
							dataSensitiveWordsVo.setCheckFlag(false);
						}
					}
				}
			}
		}
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询敏感字段表列表(带分页)",notes = "查询敏感字段表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DataSensitiveWordsVo>> queryPageList(@ModelAttribute@Valid DataSensitiveWordsQueryForm form) throws BusinessServiceException {
		logger.info("分页查询敏感字段");
		DataSensitiveWordsCondition condition = this.getConditionByQueryForm(form);
		List<DataSensitiveWordsVo> voList = new ArrayList<>();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = dspDataSensitiveWordsService.queryCount(condition);
		if (count > BusinessConstants.NumberConstant.ZERO) {
			List<DataSensitiveWordsPo> poList = dspDataSensitiveWordsService.queryList(condition);
			voList = CopyUtil.transfer(poList, DataSensitiveWordsVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增敏感字段表",notes = "新增敏感字段表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataSensitiveWordsVo> add(@ModelAttribute@Valid DataSensitiveWordsCreateForm form) throws BusinessServiceException {
		logger.info("新增敏感字段表");
		DataSensitiveWordsPo po = CopyUtil.transfer(form, DataSensitiveWordsPo.class);
		dspDataSensitiveWordsService.insert(po);
		DataSensitiveWordsVo vo = CopyUtil.transfer(po, DataSensitiveWordsVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改敏感字段表",notes = "修改敏感字段表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DataSensitiveWordsUpdateForm form) throws BusinessServiceException {
		DataSensitiveWordsPo po = CopyUtil.transfer(form, DataSensitiveWordsPo.class);
		dspDataSensitiveWordsService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除敏感字段表",notes = "删除敏感字段表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "word_id", required = true)@RequestParam  List<Integer> wordId) throws BusinessServiceException {
		logger.info("删除敏感字段表");
		for (Integer w:wordId
			 ) {
			DataTypeWordRelCondition dataTypeWordRelCondition = new DataTypeWordRelCondition();
			dataTypeWordRelCondition.setWordId(w);
			List<DataTypeWordRelPo> dataTypeWordRelPos = dataTypeWordRelService.queryList(dataTypeWordRelCondition);
			if(!CollectionUtils.isEmpty(dataTypeWordRelPos)){
				for (DataTypeWordRelPo dataTypeWordRelPo:dataTypeWordRelPos
					 ) {
					DataTypePo dataTypePo = dataTypeService.query(dataTypeWordRelPo.getTypeId());
					if(dataTypePo!=null){
						return getFailResult(dspDataSensitiveWordsService.query(w).getName().concat("已绑定").concat(dataTypePo.getTypeCode().concat(dataTypePo.getTypeName())));
					}
				}
			}
			//支持批量
			dspDataSensitiveWordsService.delete(w);
		}
		return getSuccessResult();
	}

	/**
	 * DspDataSensitiveWordsQueryForm转换为DspDataSensitiveWordsCondition
	 *
	 * @param form
	 * @return
	 */
	private DataSensitiveWordsCondition getConditionByQueryForm(DataSensitiveWordsQueryForm form) {
		DataSensitiveWordsCondition condition = CopyUtil.transfer(form, DataSensitiveWordsCondition.class);
		return condition;
	}

}